<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Core\BaseController;
use App\Models\Sms;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Input;

class SmsController extends BaseController
{

    /**
     * 公共部分发送验证码
     * @param Request $request
     * @return void
     */
    public function index(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'mobile' => 'required|min:11|string',
        ]);
        if ($validator->fails()) {

            return $this->retRequestJson(1, '手机格式错误');
        }

        //判断是否在规定时间内进行验证
        $now = time();
        $express = $now - 600;
        $sms = Sms::where('create_time', '>', $express)->limit(1)->get();
        if ($sms->first()) {
            return $this->retRequestJson(2, '暂时不能发送信息');
        }
        /*
         * 发送短信信息
         * */
        $code = rand(100000, 999999);
        $code = 1;
        //模拟发送
        $model = new Sms();
        $model->mobile = $request->get('mobile');
        $model->code = $code;
        $model->message = '';
        $model->create_time = $now;
        if ($model->save()) {
            return $this->retRequestJson(0, '成功');
        } else {
            return $this->retRequestJson(4, '失败');
        }
    }

}